package cn.gyx.hrm.service.impl;

import cn.gyx.hrm.constants.BaseConstants;
import cn.gyx.hrm.constants.ErrorCode;
import cn.gyx.hrm.domain.Employee;
import cn.gyx.hrm.domain.Login;
import cn.gyx.hrm.domain.Tenant;
import cn.gyx.hrm.dto.LoginAndMealDto;
import cn.gyx.hrm.dto.TenantRegisterDto;
import cn.gyx.hrm.feign.client.LoginFeignClient;
import cn.gyx.hrm.mapper.TenantMapper;
import cn.gyx.hrm.service.ITenantService;
import cn.gyx.hrm.util.AjaxResult;
import cn.gyx.hrm.util.AssertUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gyx
 * @since 2021-11-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private EmployeeServiceImpl employeeService;

    @Autowired
    private LoginFeignClient loginFeignClient;

    /**
     * 机构入驻
     * @param dto
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void entering(TenantRegisterDto dto) {
        // 获取4个参数做非空判断
        Employee employee = dto.getEmployee();// 员工信息
        Login login = dto.getLogin();// 登录信息
        Long mealId = dto.getMealId();//  套餐id
        Tenant tenant = dto.getTenant();//机构id

         //用户名判断
//        if (StringUtils.isEmpty(login.getUsername())){
//            throw new RuntimeException("用户名不能为空");
//        }

        AssertUtil.isNotEmpty(login.getUsername(), ErrorCode.LOGIN_USERNAME_IS_NULL.getFullMes());
        AssertUtil.isNotEmpty(login.getPassword(),ErrorCode.LOGIN_PASSWORD_IS_NULL.getFullMes() );
        AssertUtil.isNotNull(tenant.getCompanyName(),ErrorCode.TENANT_IS_NULL.getFullMes() );
//        if (StringUtils.isEmpty(employee)){
//            throw new RuntimeException("员工不能为空！")
//        }

        // 远程保存login表使用openfeign
//        loginFeignClient.save(login);
        login.setType(BaseConstants.Login.STATE_SYSTEM);// 默认后台用户
        LoginAndMealDto loginAndMealDto = new LoginAndMealDto();
        loginAndMealDto.setLogin(login);
        loginAndMealDto.setMealId(mealId);
        AjaxResult ajaxResult = loginFeignClient.saveLoginAndMealId(loginAndMealDto);
        if (!ajaxResult.isSuccess()||ajaxResult.getResultObj() == null) {
            throw new RuntimeException("登陆保存失败");
        }
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());

        // 保存管理员employee
        Employee emp = new Employee();
        emp.setEmail(employee.getEmail());// 邮箱
        emp.setTel(employee.getTel());// 电话
        emp.setLoginId(employee.getLoginId());
        emp.setInputTime(new Date());// 注册时间
        emp.setType(BaseConstants.Emp1.TYPE_ORG_MANAGEMENT);// 机构管理员
        employeeService.insert(employee);

        //保存机构
        tenant.setRegisterTime(new Date());// 注册时间
        tenant.setState(BaseConstants.tenant.STATE_TO_BE_REVIEWED);//默认待审核
        tenant.setAdminId(emp.getId());
        insert(tenant);
    }

}
